/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.container.zone;

import java.io.IOException;

import com.argot.ResourceDictionaryLoader;
import com.argot.TypeException;
import com.argot.TypeInputStream;
import com.argot.TypeLibrary;
import com.argot.TypeOutputStream;
import com.argot.TypeReader;
import com.argot.TypeReaderAuto;
import com.argot.TypeWriter;
import com.argot.auto.TypeSimpleWriter;
import com.argot.meta.MetaIdentified;
import com.argot.remote.MetaInterface;
import com.colony.zone.CRL;
import com.colony.zone.IRealm;
import com.colony.zone.IZone;
import com.colony.zone.process.HashMapRealm;
import com.colony.zone.process.HashMapZone;
import com.colony.zone.process.SystemRealm;

public class ZoneLoader
extends ResourceDictionaryLoader
{
	public static final String DICTIONARY = "zone.dictionary";
	
	public ZoneLoader()
	{
		super( DICTIONARY );
	}
	
	public String getName()
	{
		return DICTIONARY;
	}
	
	public void bind( TypeLibrary library )
	throws TypeException
	{
		library.bind(CRL.TYPENAME, new TypeReaderAuto( CRL.class ), new TypeSimpleWriter(new CRL.CRLTypeWriter()), CRL.class );
		library.bind("any", new AnyReader(), new AnyWriter(), Object.class );
		library.bind(IZone.TYPENAME, new MetaInterface.MetaInterfaceReader(), new MetaInterface.MetaInterfaceWriter(),IZone.class );
		library.bind(IRealm.TYPENAME, new MetaInterface.MetaInterfaceReader(), new MetaInterface.MetaInterfaceWriter(),IRealm.class );
		library.addClassAlias(library.getId(IZone.TYPENAME), HashMapZone.class);
		library.addClassAlias(library.getId(IRealm.TYPENAME), HashMapRealm.class);
		library.addClassAlias(library.getId(IRealm.TYPENAME), SystemRealm.class);		
	}

	private static class AnyWriter
	implements TypeWriter
	{
		public void write(TypeOutputStream out, Object o)
		throws TypeException, IOException 
		{
			MetaIdentified identified = new MetaIdentified("object");
			identified.getWriter(out.getTypeMap()).write(out, o);
		}
	}
	
	private static class AnyReader
	implements TypeReader
	{
		public Object read(TypeInputStream in)
		throws TypeException, IOException 
		{
			MetaIdentified identified = new MetaIdentified("object");
			return identified.getReader(in.getTypeMap()).read(in);
		}
	}	

}
